#include <sstream>
#include <fstream>
#include <glog/logging.h>
#include "basic/tools.h"

unsigned int generateCRC(const unsigned int timestamp) {
    unsigned int crc = 0;
    unsigned int tmp[4] = {
        timestamp & 0xF000,
        timestamp & 0x0F00,
        timestamp & 0x00F0,
        timestamp & 0x000F
    };

    for (int i = 0; i < 4; i++) {
        crc ^= tmp[i];
        for (int n = 0; n < 8; n++) {
            if ((crc & 0x80) != 0) {
                crc = (crc << 1) ^ 0xB9;
            } else {
                crc <<= 1;
            }
        }
    }
    return crc & 0xFF;
}

void curl_image(const std::string image) {
    std::string cmd = CURL_CMD[1] + image + curl_ipv4;
    std::system(cmd.c_str());
    // execlp
}

void curl_image(const std::string image, const std::string yaml) {
    std::string cmd = CURL_CMD[0] + image + CURL_CMD[2] + yaml + curl_ipv4;
    // LOG(INFO) << cmd;
    std::system(cmd.c_str());
}

bool isFileExists(const std::string& filename) {
    std::ifstream file(filename.c_str());
    return file.good();
}